using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmRollcall : Form
    {
        List<Entities.Course> courseList = new List<Entities.Course>();
        List<Entities.Class> classList = new List<Entities.Class>();
        List<Entities.Student> studentList = new List<Entities.Student>();
        List<Entities.Rollcall> rollcallList = new List<Entities.Rollcall>();
        Business.RollcallService rollcallService = new Business.RollcallService();
        Business.CourseService courseService = new Business.CourseService();
        Business.ClassService classService = new Business.ClassService();
        Business.StudentService studentService = new Business.StudentService();
        Entities.Rollcall rollcall = new Entities.Rollcall();
        int iIndex = 0;
        
        public FrmRollcall()
        {
            InitializeComponent();
        }

        private void InitializeCourse()
        {
            courseList = courseService.GetAllCourses();
            foreach (Entities.Course course in courseList)
            {
                cmbCourse.Items.Add(course.CourseID + "--" + course.CourseName);
            }
        }

        private void InitializeClass()
        {
            cmbClass.Items.Clear();
            if (cmbCourse.Text.Length > 0)
            {
                classList = classService.GetClassByCourseID(cmbCourse.Text.Substring(0,5));
                foreach (Entities.Class _class in classList)
                {
                    cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
                }
            }
            else
            {
                classList = classService.GetAllClasses();
                foreach (Entities.Class _class in classList)
                {
                    cmbClass.Items.Add(_class.ClassID + "--" + _class.ClassName);
                }
            }
        }

        private void InitializeStudent()
        {
            studentList = studentService.GetStudentsByClassID(cmbClass.Text.Substring(0,6));
            foreach (Entities.Student student in studentList)
            {
                cmbStudent.Items.Add(student.StudentID );
            }
        }

        private void ShowDataGirdView()
        {

            if (cmbCourse.Text != string.Empty )
            {
                using (DataTable dataTable = rollcallService.GetRollcallsByCourseID(cmbCourse.Text.Substring(0, 5)))
                {
                    SetColumnForDataGird(dataTable);
                }
                
            }
            else
            {
                using (DataTable dataTable = rollcallService.GetRollcallsByClassID(cmbClass.Text.Substring(0, 6)))
                {
                    SetColumnForDataGird(dataTable);
                }
            }
            dgvRollcall[0, iIndex].Selected = true;
        }

        private void ShowDataGirdView1()
        {
            using (DataTable dataTable = rollcallService.GetRollcallByStudentID(cmbStudent.Text.Substring(0, 9)))
            {
                SetColumnForDataGird(dataTable);
            }
        }

        private void ShowDataGirdView2()
        {
            using (DataTable dataTable = rollcallService.GetRollcallsByRollcallDay(dtpDay.Value.Date))
            {
                //dgvRollcall.DataSource = dataTable.DefaultView;
                SetColumnForDataGird(dataTable);
            }
        }

        private void SetColumnForDataGird(DataTable dataTable)
        {
            if (dgvRollcall.Rows.Count > 1)
            {
                int iCount = dgvRollcall.Rows.Count-1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvRollcall.Rows.RemoveAt(0);
                }
            }
            foreach (DataRow row in dataTable.Rows)
            {
                string[] temp = { row[0].ToString(), row[1].ToString(), ((DateTime)row[2]).ToShortDateString(), int.Parse(row[3].ToString()) ==1?"True":"False" };
                dgvRollcall.Rows.Add(temp);
            }
            dgvRollcall.Rows[dgvRollcall.Rows.Count - 1].Cells[3].ReadOnly = true;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (cmbCourse.Text == string.Empty)
                sInformation += "Course is empty" + "\n";
            if (cmbClass.Text == string.Empty)
                sInformation += "Class is empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void UpdateData()
        {
            for (int i = 0; i < dgvRollcall.Rows.Count - 1; i++)
            {
                string sStudentID = dgvRollcall.Rows[i].Cells[0].Value.ToString();
                DateTime dRollcallDay = DateTime.Parse(dgvRollcall.Rows[i].Cells[2].Value.ToString());
                Entities.Rollcall rollcall = rollcallService.GetRollcallByStudentIDAndRollcallDay(sStudentID, dRollcallDay);
                if (dgvRollcall[3, i].Value.ToString() == "True")
                    rollcall.Attend = 1;
                else
                    rollcall.Attend = 0;

                rollcallService.UpdateRollcall(rollcall);

            }
            ShowDataGirdView();
        }

        private void InsertData()
        {
            if (Test())
            {
                if (MessageBox.Show("You make a rollcall for " + cmbClass.Text.Substring(0, 6) + " on " + dtpDay.Value.ToShortDateString() + ".Are you sure?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    for (int i = 0; i < studentList.Count; i++)
                    {
                        Entities.Rollcall rollcall = new Entities.Rollcall();

                        rollcall.StudentID = studentList[i].StudentID;
                        rollcall.RollcallDay = dtpDay.Value.Date;
                        rollcall.Attend = 0;
                        try
                        {
                            rollcallService.InsertRollcall(rollcall);
                        }
                        catch
                        {
                            MessageBox.Show("Data have existed");
                        }
                        iIndex = dgvRollcall.Rows.Count - 1;
                    }
                    ShowDataGirdView();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dtpDay.Value.ToShortDateString());
        }

        private void FrmRollcall_Load(object sender, EventArgs e)
        {
            InitializeCourse();
        }

        private void cmbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeClass();
            ShowDataGirdView();
        }

        private void cmbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeStudent();
            ShowDataGirdView();
        }

        private void dtpDay_ValueChanged(object sender, EventArgs e)
        {
            ShowDataGirdView2();
        }

        private void cmbStudent_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowDataGirdView1();
        }

        private void rdoInsert_CheckedChanged(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            cmbStudent.Enabled = false;
        }

        private void rdoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            btnInsert.Enabled = false;
            btnUpdate.Enabled = true;
            cmbStudent.Enabled = true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for(int i=0;i<dgvRollcall.Rows.Count-1;i++)
            {
                MessageBox.Show(dgvRollcall.Rows[i].Cells[0].Value.ToString() +"---"+ dgvRollcall[3,i].Value.ToString());
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvRollcall_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            iIndex = dgvRollcall.CurrentRow.Index;
        }
    }
}